var base = require('/base_info.js');
var utils = require('/utils/index.js');
// 富文本解析
var wxParse = require('/pages/wxParse-master/wxParse.js');
Page({
  data: {
    forward_invoice_type: 2,
    forward_invoice_url: "",
    serverList: [],
    advert: {},
    imgUrl: base.imgUrl,
    modelSign: false
  },
  // 点击模态框
  modelClick () {
    this.setModel();
  },
  contentClick () {

  },
  closeModel () {
    this.setModel();
  },
  // 模态框显隐
  setModel () {
    this.setData({
      modelSign: !this.data.modelSign
    })
  },
  // banner点击跳转
  bannerNavigator () {
    var type = this.data.advert.state;
    var that = this;
    if (type == 1) { // 内部跳转
      my.navigateTo({
        url: that.data.advert.src
      });
    } else { // 跳转到其他小程序
      my.navigateToMiniProgram({
        appId: that.data.advert.appid,
        path: that.data.advert.src,
        success: (res) => {
          console.log(JSON.stringfy(res))
        },
        fail: (res) => {
          console.log(JSON.stringfy(res))
        }
      });
    }
  },
  // 点击服务选项
  serverItemClick (e) {
    var index = e.currentTarget.dataset.index;
    var serverList = this.data.serverList;
    if (serverList[index].url_type == 1) {
      // 小程序内跳转
      my.navigateTo({
        url: serverList[index].url
      });
    } else if (serverList[index].url_type == 2) {
      // 显示弹窗
      this.setModel();
      var str = "<p class='text-center' style='color: #999;'>暂不提供该功能</p>";
      wxParse.wxParse('content', 'html', !serverList[index].content || serverList[index].content == null || serverList[index].content == "" ? str : serverList[index].content, this, 5);
    } else if (serverList[index].url_type == 3) {
      // 跳转到其他小程序
      my.navigateToMiniProgram({
        appId: serverList[index].appid,
        path: serverList[index].url,
        success: (res) => {
          console.log(JSON.stringfy(res))
        },
        fail: (res) => {
          console.log(JSON.stringfy(res))
        }
      });
    }
  },
  // 发票跳转
  invoiceNavigator () {
    if (this.data.forward_invoice_type == 1) {
      my.navigateTo({
        url: './../web-view/web-view?weburl=' + this.data.forward_invoice_url
      });
      return false;
    }
    my.navigateTo({
      url: './invoice'
    });
  },
  // 吐槽跳转
  teaseNavigator (e) {
    my.navigateTo({
      url: './../tease/tease' 
    })
  },
  // 导览跳转
  pandectNavigator () {
    my.navigateTo({
      url: './pandect'
    })
  },
  // 服务菜单选项查询
  InvoiceType () {
    let that = this;
    utils.getData("SingServiceType", {}, function (res) {
      my.hideLoading();
      console.log(res.data.data);
      that.setData({
        serverList: res.data.data.server,
        advert: res.data.data.advert
      });
    }, function(err) {
      my.hideLoading();
      console.log(err);
    });
    // 查询发票跳转类型
    // utils.getData("SingleForwardInvoiceType", {}, function (res) {
    //   console.log(res);
    //   if (res.data.data.forward_invoice_type == 1) {
    //     that.setData({
    //       forward_invoice_type: 1,
    //       forward_invoice_url: res.data.data.forward_invoice_url
    //     });
    //   }
    // }, function(err) {
    //   console.log(err);
    // })
  },
  onLoad(query) {
    // 页面加载时
    let that = this;
    my.showLoading();
    var uid = my.getStorageSync({
      key: 'uid', // 缓存数据的key
    }).data;
    that.InvoiceType();
    if (!uid || uid == "") {
      utils.getAuthCode(function(){});
    };
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭 
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
